package com.kolon.business.mOI.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mOI.controller.MOIDeptListController")
public class MOIDeptListController implements SimpleController {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/business/mOI/mOIDeptList.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 회원정보 세션
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String userId = (String)userMap.get("userId");
		String userPw = (String)userMap.get("userPw");
		String deptCode = ComUtil.nullChk(request.getParameter("deptCode"));
		String cpyCD = (String)userMap.get("CompanyCD");
		// XML DOCUMENT 및 인증 처리
		StringBuffer buffer = new StringBuffer();
		Map deptMap = new HashMap();
		Map map = null;
		List deptList = new ArrayList();
		buffer.append(url);
		buffer.append("/oi_suggestion.aspx?strGubun=selJudge&mode=judmanager&Page=1&PageFlag=e1&companycd=").append(cpyCD);
		buffer.append("&UserID=&UserName=&DeptCode=").append(deptCode);
		buffer.append("&DeptName=&HeadFlag=&CLJudgeYN=N&AdminYN=N&EnvironmentYN=N");
		buffer.append("&ViewRowCount=999");
		buffer.append("&search1=&search2=&search_txt=");
		Document doc = ComUtil.getDocument(buffer.toString(), userId, userPw);
		Element element = doc.getRootElement();
		List xmlList = element.getChildren("Admin_Jud_Manager_T");
		Iterator itXml = xmlList.iterator();
		if ("".equals(deptCode)) {
			while (itXml.hasNext()) {
				map = new HashMap();
				Element el = (Element)itXml.next();
				if (!deptMap.containsKey(el.getChild("deptcd").getText())) {
					map.put("companycd", el.getChild("companycd").getText());
					map.put("companyname", el.getChild("companyname").getText());
					map.put("parentdeptname ", el.getChild("parentdeptname").getText());
					map.put("deptname", el.getChild("deptname").getText());
					map.put("deptcd", el.getChild("deptcd").getText());
					map.put("empid", el.getChild("empid").getText());
					map.put("empname", el.getChild("empname").getText());
					deptList.add(map);
				}
				deptMap.put(el.getChild("deptcd").getText(), map);
			}
		} else {
			while (itXml.hasNext()) {
				map = new HashMap();
				Element el = (Element)itXml.next();
				map.put("companycd", el.getChild("companycd").getText());
				map.put("companyname", el.getChild("companyname").getText());
				map.put("parentdeptname ", el.getChild("parentdeptname").getText());
				map.put("deptname", el.getChild("deptname").getText());
				map.put("deptcd", el.getChild("deptcd").getText());
				map.put("empid", el.getChild("empid").getText());
				map.put("empname", el.getChild("empname").getText());
				deptList.add(map);
			}
		}
		// 리턴
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("deptList", deptList);
		return modelAndView;
	}

}
